{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "226af11f-c2ac-40d4-839b-e2531090b132",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dotenv:  E:\\work\\code\\ai\\.env\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "\n",
    "def get_openai_key():\n",
    "    print(\"dotenv: \",find_dotenv())\n",
    "    _ = load_dotenv(find_dotenv())\n",
    "    # api_key = os.environ['OPENAI_API_KEY']\n",
    "    return OpenAI(\n",
    "        api_key = os.environ.get(\"ARK_API_KEY\"),\n",
    "    base_url = \"https://ark.cn-beijing.volces.com/api/v3\",)\n",
    "\n",
    "client = get_openai_key()\n",
    "\n",
    "def get_completion_from_messages(message, model=\"ep-20250103110319-28xth\",temperature = 0, max_tokens=500):\n",
    "    # message = [\n",
    "    #     {\"role\": \"user\", \"content\": prompt},\n",
    "    # ]\n",
    "    completion = client.chat.completions.create( \n",
    "    model = model,  # your model endpoint ID\n",
    "    messages =message,\n",
    "    temperature=temperature,\n",
    "    max_tokens=max_tokens\n",
    "    )\n",
    "    return completion.choices[0].message.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67706195-da3c-4c84-aff0-8ee24f39ce7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "delimiter = \"###\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "70e3b8c6-eb4d-4b19-9e31-9cf0d7bd7f76",
   "metadata": {},
   "outputs": [],
   "source": [
    "system_message = f\"\"\"\n",
    "你将获得客户服务查询。\n",
    "每个客户查询服务都将用{delimiter} 字符分隔\n",
    "将每个查询分类到一个主要类别和一个次要类别中。\n",
    "以JSON 格式提供你的输出， 包含以下键： primary和 secondary .\n",
    "\n",
    "主要类别：计费（Billing）、技术支持（Technical Support）、账户管理（Account Management）或一般咨询（General Inquiry）。\n",
    "\n",
    "计费次要类别：\n",
    "取消订阅或升级（Unsubscribe or upgrade）\n",
    "添加付款方式（Add a payment method）\n",
    "收费解释（Explanation for charge）\n",
    "争议费用（Dispute a charge）\n",
    "\n",
    "技术支持次要类别：\n",
    "常规故障排除（General troubleshooting）\n",
    "设备兼容性（Device compatibility）\n",
    "软件更新（Software updates）\n",
    "\n",
    "账户管理次要类别：\n",
    "重置密码（Password reset）\n",
    "更新个人信息（Update personal information）\n",
    "关闭账户（Close account）\n",
    "账户安全（Account security）\n",
    "\n",
    "一般咨询次要类别：\n",
    "产品信息（Product information）\n",
    "定价（Pricing）\n",
    "反馈（Feedback）\n",
    "与人工对话（Speak to a human）\n",
    "\n",
    "\"\"\"\n",
    "user_message = f\"\"\" \n",
    "    我希望你删除我的个人资料和所有用户数据\n",
    "\"\"\"\n",
    "messages = [\n",
    "    { 'role': 'system', 'content': system_message},\n",
    "    {'role': 'user', 'content': f\"{delimiter}{user_message}{delimiter}\" }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b82a771a-c845-42a6-aba6-afeeb12a41d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"primary\": \"账户管理\",\n",
      "    \"secondary\": \"关闭账户\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = get_completion_from_messages(messages)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3428da4-2c0b-47f7-b600-62ccd42f636c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
